//
//  RootView.m
//  UI5 -UIControl课堂练习
//
//  Created by dllo on 15/11/12.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RootView.h"

@implementation RootView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}


-(void)createSubviews
{
    self.backgroundColor = [UIColor lightGrayColor];
    
    NSMutableArray *imageArr = [NSMutableArray array];
    
    for (NSInteger i = 0; i < 80; i++) {
        NSString *file = [NSString stringWithFormat:@"drink_%02ld.jpg", i];
        UIImage *image = [UIImage imageNamed:file];
        [imageArr addObject:image];
    }
    
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.frame];
    imageV.animationImages = imageArr;
    imageV.tag = 10001;
    [self addSubview:imageV];
    imageV.image = [UIImage imageNamed:@"drink_01.jpg"];
//    imageV.animationDuration = 0.2;
    
    [imageV startAnimating];
    
    [imageV release];
    
    
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 500, 300, 30)];
    
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:slider];
    
    
    slider.value = 0.5;
    
    slider.minimumTrackTintColor = [UIColor redColor];
    slider.maximumTrackTintColor = [UIColor yellowColor];
    slider.thumbTintColor = [UIColor blackColor];
    slider.maximumValue = 2.0;
    
    [slider release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 400, 50, 50);
    button.tag = 10002;
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"停止" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(250, 400, 50, 50);
    button2.backgroundColor = [UIColor blackColor];
    [button2 setTitle:@"开始" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(buttonAction2) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button2];
    
    

    
}

- (void)buttonAction2
{
    UIImageView *imageV = (UIImageView *)[self viewWithTag:10001];
    [imageV startAnimating];
}


- (void)buttonAction
{
    
    //    UIButton *button = (UIButton *)[self viewWithTag:10002];
    UIImageView *imageV = (UIImageView *)[self viewWithTag:10001];
    [imageV stopAnimating];
    
    
}

- (void)sliderAction:(UISlider *)sender
{
    NSLog(@"当前滑动条的值为:%f", sender.value);
    UIImageView *imageV = (UIImageView *)[self viewWithTag:10001];
    imageV.animationDuration = sender.maximumValue - sender.value + 0.01;
    [imageV startAnimating];
    
    
}




@end
